using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using PHSRAG.Insight21.SearchContext;
using Calendar = PHSRAG.WebControls.Calendar;
using DataAccess = PHSRAG.Utility.DataAccess;
using SessionCachePolicy = PHSRAG.Insight21.Policy.SessionCachePolicy;
using UserContext = PHSRAG.Insight21.Context.UserContext;
using Constants = PHSRAG.Insight21.InsightUtilities.Constants;

namespace PHSRAG.Insight21.InsightWebControls
{
    [ToolboxData("<{0}:PatientCareDetailsSearch runat=server></{0}:PatientCareDetailsSearch>")]
    public class PatientCareDetailsSearch : BaseSearch, INamingContainer, ISearch
    {
        #region Instance Variables
        private ControlCollection controlCollection;
        private Policy.ICachePolicy cachePolicy;
        private TextBox patientMRN;
        private TextBox patientFirstName;
        private TextBox patientLastName;
        private TextBox chargeCode;
        private TextBox institutionalMRN;
        private Calendar serviceFromDate;
        private Calendar serviceToDate;
        private Calendar postFromDate;
        private Calendar postToDate;
        #endregion

        #region ISearch Members

        public void Reset()
        {
            ResetControls(controlCollection);
        }

        public SearchCriteria SearchCriteria
        {
            get
            {
                return ((PatientCareDetailsSearchContext)cachePolicy.GetCachedObject(PatientCareDetailsSearchContext.CacheKey)).GetSearchCriteria(Page.Request.Url.ToString());
            }
        }

        public SearchCriteria UpdateSearchCriteria()
        {
            UserContext userContext = (UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey);
            return GetSearchCriteria(userContext.DomainUserId, controlCollection);
        }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            cachePolicy = (Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey];
            PatientCareDetailsSearchContext searchContext = cachePolicy.GetCachedObject(PatientCareDetailsSearchContext.CacheKey)
                as PatientCareDetailsSearchContext;
            if (searchContext == null)
            {
                searchContext = new PatientCareDetailsSearchContext(cachePolicy);
            }

            EnsureChildControls();
            base.OnInit(e);
            searchContext.GetSearchCriteria(Page.Request.Url.ToString(), controlCollection);
        }

        /// <summary>
        /// Initialize the contents of all controls on first load. If the OverviewSearchContext is not cached yet, a
        /// new instance is created and cached.
        /// </summary>
        /// <param name="e">Argument associated with the event</param>
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                cachePolicy = (Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey];

                if (!Page.IsPostBack)
                {
                    EnsureChildControls();

                    PatientCareDetailsSearchContext searchContext = cachePolicy.GetCachedObject(PatientCareDetailsSearchContext.CacheKey)
                        as PatientCareDetailsSearchContext;
                    if (searchContext == null)
                    {
                        searchContext = new PatientCareDetailsSearchContext(cachePolicy);
                        searchContext.Load(DataAccess.ConnectionString);
                        searchContext.Cache();
                    }
                    SetControlState(searchContext.GetSearchCriteria(Page.Request.Url.ToString(), controlCollection));
                }
            }
            catch (Exception ex)
            {
                throw new Exception(Utility.ExceptionReport.GetExceptionInfo(ex, false));
            }
        }

        /// <summary>
        /// Create all controls. Instantiate all the controls and generate
        /// the underlying HTML so that the controls shows up per the need.
        /// </summary>
        protected override void CreateChildControls()
        {
            CreateControls();
            GenerateControlTree();
        }

        private void CreateControls()
        {
            Controls.Clear();
            controlCollection = new ControlCollection(this);

            patientMRN = new TextBox();
            patientMRN.ID = Constants.PatientMRNKey;
            patientMRN.CssClass = "DataInput";
            patientMRN.Attributes["Label"] = "Patient MRN";
            controlCollection.Add(patientMRN);

            patientFirstName = new TextBox();
            patientFirstName.ID = Constants.FirstNameKey;
            patientFirstName.CssClass = "DataInput";
            patientFirstName.Attributes["Label"] = "Patient First Name";
            controlCollection.Add(patientFirstName);

            patientLastName = new TextBox();
            patientLastName.ID = Constants.LastNameKey;
            patientLastName.CssClass = "DataInput";
            patientLastName.Attributes["Label"] = "Patient Last Name";
            controlCollection.Add(patientLastName);

            chargeCode = new TextBox();
            chargeCode.ID = Constants.ChargeCodeKey;
            chargeCode.CssClass = "DataInput";
            chargeCode.Attributes["Label"] = "Charge Code";
            controlCollection.Add(chargeCode);

            institutionalMRN = new TextBox();
            institutionalMRN.ID = Constants.InstitutionalMRNKey;
            institutionalMRN.CssClass = "DataInput";
            institutionalMRN.Attributes["Label"] = "Patcom/BICS #";
            controlCollection.Add(institutionalMRN);

            serviceFromDate = new Calendar();
            serviceFromDate.ID = Constants.ServiceFromDateKey;
            serviceFromDate.CssClass = "DataInput";
            serviceFromDate.Prompt = CommonSearch.SelectPrompt;
            serviceFromDate.Attributes["Label"] = "Service From Date";
            controlCollection.Add(serviceFromDate);

            serviceToDate = new Calendar();
            serviceToDate.ID = Constants.ServiceToDateKey;
            serviceToDate.CssClass = "DataInput";
            serviceToDate.Prompt = CommonSearch.SelectPrompt;
            serviceToDate.Attributes["Label"] = "Service To Date";
            controlCollection.Add(serviceToDate);

            postFromDate = new Calendar();
            postFromDate.ID = Constants.PostFromDateKey;
            postFromDate.CssClass = "DataInput";
            postFromDate.Prompt = CommonSearch.SelectPrompt;
            postFromDate.Attributes["Label"] = "Post From Date";
            controlCollection.Add(postFromDate);

            postToDate = new Calendar();
            postToDate.ID = Constants.PostToDateKey;
            postToDate.CssClass = "DataInput";
            postToDate.Prompt = CommonSearch.SelectPrompt;
            postToDate.Attributes["Label"] = "Post To Date";
            controlCollection.Add(postToDate);
        }

        private void GenerateControlTree()
        {
            string searchClientId = ((Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey]).GetCachedObject(Constants.SearchButtonClientIDKey) as string;
            if (searchClientId == null)
                searchClientId = "expandingPanel_commonSearch_search";

            Table t1 = new Table();
            t1.Width = Unit.Percentage(100);
            t1.CssClass = "Search";
            t1.Attributes.Add("onkeypress", "return ClickButton(event, '" + searchClientId + "')");
            Controls.Add(t1);

            TableRow row = new TableRow();
            t1.Rows.Add(row);

            TableCell cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "MRN #:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(patientMRN);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Patient Last Name:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(patientLastName);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Patient First Name:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(patientFirstName);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Charge Code:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(chargeCode);
            row.Cells.Add(cell);

            row = new TableRow();
            t1.Rows.Add(row);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Post Date From:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(postFromDate);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Post Date To:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(postToDate);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Service Date From:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(serviceFromDate);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Service Date To:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(serviceToDate);
            row.Cells.Add(cell);

            row = new TableRow();
            t1.Rows.Add(row);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.Text = "Patcom/BICS #:\t\t";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Left;
            cell.Controls.Add(institutionalMRN);
            row.Cells.Add(cell);

        }
    }
}
